﻿Ext.data.Proxy = Ext.extend(Ext.util.Observable, {

    batchOrder: 'create,update,destroy',


    defaultReaderType: 'json',


    defaultWriterType: 'json',



    constructor: function (config) {
        config = config || {};

        if (config.model == undefined) {
            delete config.model;
        }

        Ext.data.Proxy.superclass.constructor.call(this, config);

        if (this.model != undefined && !(this.model instanceof Ext.data.Model)) {
            this.setModel(this.model);
        }
    },

    //设置与代理相关的模型. 这个方法是提供给Store使用的
    setModel: function (model, setOnStore) {
        this.model = Ext.ModelMgr.getModel(model);//获取model类

        var reader = this.reader,
            writer = this.writer;

        this.setReader(reader);//设置Reader实例，如果reader未定义，则自动定义
        this.setWriter(writer);//设置Writer实例，如果writer未定义，则自动定义

        if (setOnStore && this.store) {
            this.store.setModel(this.model);
        }
    },

    //获取与当前代理关联的模型
    getModel: function () {
        return this.model;
    },

    //设置代理的Reader类, 参数可以是字符串、配置对象或Reader类实例
    setReader: function (reader) {
        if (reader == undefined || typeof reader == 'string') {//如果参数是字符串，则用于构造配置对象
            reader = {
                type: reader
            };
        }

        if (reader instanceof Ext.data.Reader) {//如果参数是Reader类实例，则为Reader类实例设置与代理关联的模型
            reader.setModel(this.model);
        } else {//如果参数是一个Reader的配置对象，则把当前代理、关联模型、默认Reader类型补充给它
            Ext.applyIf(reader, {
                proxy: this,
                model: this.model,
                type: this.defaultReaderType
            });

            reader = Ext.data.ReaderMgr.create(reader);//创建Reader类实例
        }

        this.reader = reader;//将Reader的实例赋给当前代理的reader属性

        return this.reader;//返回Reader属性
    },

    //获取当前代理的Reader实例
    getReader: function () {
        return this.reader;
    },

    //设置代理的Writer类, 参数可以是字符串、配置对象或Writer类实例
    setWriter: function (writer) {
        if (writer == undefined || typeof writer == 'string') {//如果参数是字符串，则用于构造配置对象
            writer = {
                type: writer
            };
        }

        if (!(writer instanceof Ext.data.Writer)) {//如果参数是Writer配置对象，
            Ext.applyIf(writer, {//则把当前代理的关联模型、默认Writer类型补充给它
                model: this.model,
                type: this.defaultWriterType
            });

            writer = Ext.data.WriterMgr.create(writer);//创建Writer类实例
        }

        this.writer = writer;//将Writer类实例赋给当前代理的writer属性

        return this.writer;
    },

    //获取当前代理的Writer实例
    getWriter: function () {
        return this.writer;
    },


    create: Ext.emptyFn,


    read: Ext.emptyFn,


    update: Ext.emptyFn,


    destroy: Ext.emptyFn,

    // 批量操作
    batch: function (operations, listeners) {
        var batch = new Ext.data.Batch({
            proxy: this,
            listeners: listeners || {}
        });

        Ext.each(this.batchOrder.split(','), function (action) {
            if (operations[action]) {
                batch.add(new Ext.data.Operation({
                    action: action,
                    records: operations[action]
                }));
            }
        }, this);

        batch.start();

        return batch;
    }
});


Ext.data.DataProxy = Ext.data.Proxy;

Ext.data.ProxyMgr.registerType('proxy', Ext.data.Proxy);